package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.User;
import com.conversationboard.tree.Folders;

@WebServlet(name = "AdminRemoveNodeFromFolderControllerServlet", urlPatterns = "/Admin/AdminRemoveNodeFromFolderControllerServlet")
public class AdminRemoveNodeFromFolderControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -5585405304216563654L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal().getName());

			if (!(user.isSiteAdmin()) || (!SecurityChecks.passes(request, user, this))) {
				return;
			}

			int nodeId = Integer.parseInt(request.getParameter("nodeId"));

			Folders.getInstance().removeNode(user, nodeId);

			// int parentFolderId =
			// Integer.parseInt(request.getParameter("parentFolder"));
			//
			// Node parentFolder =
			// Folders.getInstance().getRoot().findFolder(parentFolderId);
			//
			// parentFolder.removeChildBoard(boardId, false);

			Folders.getInstance().store();

			response.sendRedirect(Configuration.getInstance().getRoot() + "/Pages/Admin/boardstructure.jsp");

		} catch (SQLException e) {
			throw new ServletException(e);
		}
	}

}